package com.view.purchasemanage;

import com.bean.*;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoLabel;
import com.lovo.netCRM.component.LovoTable;
import com.service.PurchaseService;
import com.service.SupplierService;
import com.service.impl.PurchaseServiceImpl;
import com.service.impl.SupplierServiceImpl;
import com.util.PageUtilSuper;
import com.view.MainFrame;
import com.view.jurisdictionmanage.RoleShowDialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class PurInManagePanel extends JPanel {
    private LovoComboBox<Supplier> supplierComb;
    private LovoLabel principal;
    private LovoLabel inGoods;

    private MainFrame frame;

    private LovoTable purInTab;

    private LovoButton addGoodsBtn;
    private LovoButton delGoodsBtn;
    private LovoButton inGoodsBtn;
    private LovoButton updateBtn;
    private Purchase purchase;
    private User user;

    private SupplierService supplierService = new SupplierServiceImpl();
    private PurchaseService purchaseService = new PurchaseServiceImpl();


    public PurInManagePanel(MainFrame frame){
        this.frame = frame;
        user = frame.getUser();
        this.setBackground(Color.WHITE);
        this.setLayout(null);
        cmbFlush();
        this.init();
        this.initTable();

    }

    public Purchase getPurchase(){
        User user = frame.getUser();
        Supplier supplier = supplierComb.getItem();
        Purchase purchase = purchaseService.purchaseInit(user,supplier);
        return purchase;
    }

    public void tableFlush(){
        List<PurchaseInfo> purchases = new ArrayList<>();
        Supplier supplier = supplierComb.getItem();
        purchases = purchaseService.getAllGoods(frame.getUser(),supplier);
        purInTab.updateLovoTable(purchases);
    }

    private void initTable(){
        this.purInTab = new LovoTable(this, new String[]{"商品名称","厂家","建议采购价","建议销售价","采购数量"},
                new String[]{"goods.name","goods.manufactor","goods.buyPrice","goods.sellPrice","number"},//数组中应该是对应属性的属性名
                "id");//填入唯一标示属性
        List<PurchaseInfo> purchases = new ArrayList<>();
        Supplier supplier = supplierComb.getItem();
        purchases = purchaseService.getAllGoods(frame.getUser(),supplier);
        purInTab.updateLovoTable(purchases);
        this.purInTab.setSizeAndLocation(20, 150, 750, 200);
        //bindTable();
    }

    public void cmbFlush(){
        List<Supplier> suplist = new ArrayList<>();
        suplist = supplierService.getAllSupplier();
        this.supplierComb = new LovoComboBox<>("供应商", suplist,50,50,this);
        supplierComb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<PurchaseInfo> purchases = new ArrayList<>();
                Supplier supplier = supplierComb.getItem();
                purchases = purchaseService.getAllGoods(frame.getUser(),supplier);
                purInTab.updateLovoTable(purchases);
            }
        });
    }

    private void init(){
        String userName = user.getRealName();
        this.principal = new LovoLabel("<html>负责人  "+userName+"</html>",350,50,this);
        this.inGoods = new LovoLabel("采购物品",50,100,this);

        this.addGoodsBtn = new LovoButton("添加商品", 100, 400, this);
        this.delGoodsBtn = new LovoButton("移除商品", 250, 400, this);
        this.updateBtn = new LovoButton("修改数量", 400, 400, this);
        this.inGoodsBtn = new LovoButton("入库",550,400,this);

        addGoodsBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ;GoodsAddDialog goodsAddDialog = new GoodsAddDialog(frame);
            }
        });

        delGoodsBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = purInTab.getKey();
                if(key == -1 ){
                    JOptionPane.showMessageDialog(null,"请先选择一行");
                }else {
                    int m = JOptionPane.showConfirmDialog(null,"你确定要删除吗？");
                    if(m == 0 ){
                        if(purchaseService.deleteGoods(key)){
                            JOptionPane.showMessageDialog(null,"删除成功");
                            frame.flush();
                        }else {
                            JOptionPane.showMessageDialog(null,"删除失败");
                        }
                    }
                }            }
        });

        updateBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = purInTab.getKey();
                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选择一行");
                }else{
                    GoodsUpdateDialog goodsUpdateDialog = new GoodsUpdateDialog(frame,key);
                }
            }
        });

        inGoodsBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Supplier supplier = supplierComb.getItem();
                if(purchaseService.inWarehouse(user,supplier)){
                    JOptionPane.showMessageDialog(null,"入库成功");
                    frame.flush();
                }else {
                    JOptionPane.showMessageDialog(null,"入库失败");
                }
            }
        });



    }
}
